package com.mzy.springboot;

import com.mzy.springboot.bean.Cat;
import com.mzy.springboot.bean.Dog;
import com.mzy.springboot.bean.Monster;
import com.mzy.springboot.config.BeanConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;

// 用来表示这是一个 springboot 程序
// scanBasePages属性是用来指定扫描的包的位置
@SpringBootApplication(scanBasePackages = {"com.mzy"})
public class MainApp {

    public static void main(String[] args) {
        // 启动 springboot 应用程序，它可以返回 ioc 容器
        ConfigurableApplicationContext ioc = SpringApplication.run(MainApp.class, args);
        String[] beanDefinitionNames = ioc.getBeanDefinitionNames();
        Arrays.stream(beanDefinitionNames).forEach(System.out::println);

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        // Monster monster = applicationContext.getBean("monster", Monster.class);

       /* Monster monster = applicationContext.getBean("monster", Monster.class);
        Monster monster01 = ioc.getBean("monster01", Monster.class);
        Monster monster02 = ioc.getBean("monster01", Monster.class);
        System.out.println(monster.hashCode());
        System.out.println(monster01.hashCode());
        System.out.println(monster02.hashCode());*/

        // 配置类也会被注入到 ioc 容器中
        BeanConfig beanConfig = ioc.getBean(BeanConfig.class);
        System.out.println(beanConfig);


        Monster monster01 = beanConfig.monster01();
        Monster monster02 = beanConfig.monster01();
        System.out.println(monster01.hashCode());
        System.out.println(monster02.hashCode());


        // 测试 @Import 注解
        // Dog dogBean = ioc.getBean(Dog.class);
        // Cat catBean = ioc.getBean(Cat.class);
        //
        // System.out.println(dogBean);
        // System.out.println(catBean);

        // 测试 Condition 注解
        // Dog dog = ioc.getBean(Dog.class);
        // System.out.println(dog);

        System.out.println(ioc.containsBean("monster_xml"));
        System.out.println(ioc.getBean("monster_xml", Monster.class));
    }
}
